package com.tr.rentcar.controller.houtai.employeemanage;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import com.tr.rentcar.model.dao.EmployeeDao;
import com.tr.rentcar.common.FileUpload;
import com.tr.rentcar.common.IDCardValidate;
import com.tr.rentcar.model.bean.Employee;

@WebServlet({ "/employtianjia/*", "/employtianjia" })
@MultipartConfig(maxFileSize = 10 * 1024 * 1024)
public class EmploytianjiaControllor extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.getRequestDispatcher("/WEB-INF/jsp/houtai_employtianjia.jsp").forward(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String ename = req.getParameter("ename");
		String eidentity = req.getParameter("eidentity");
		String etel = req.getParameter("etel");
		String ebir = req.getParameter("ebir");
		String estatus = req.getParameter("selectname");
		IDCardValidate idcard = new IDCardValidate();
		if (ename.isEmpty() || eidentity.isEmpty() || etel.isEmpty() || ebir.isEmpty()) {
			req.setAttribute("eidentityerror", "信息不完善");
			doGet(req, resp);
			return;
		}
		if (!idcard.validate(eidentity)) {
			req.setAttribute("eidentityerror", "身份证不正确");
			doGet(req, resp);
			return;
		}
		;
		// 获取头像Part对象,上传图片
		Part part = req.getPart("tx");
		// String
		// fileSavePath="D:/Users/Administrator/git/rentcar/WebContent/statics/image/upload/employee";
		String fileSavePath = "F:/git/rentcar/WebContent/statics/image/upload/employee";
		String fileName = FileUpload.upload(part, fileSavePath);
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date date = sdf.parse(ebir);
			new EmployeeDao().save(ename, eidentity, etel, date, estatus, fileName);
		} catch (Exception e) {
			req.getRequestDispatcher("/WEB-INF/jsp/error.jsp").forward(req, resp);
			e.printStackTrace();
		}
		resp.sendRedirect(req.getContextPath() + "/houtaiemployeeManage");
	}

}
